<?php

include_once("../service/productService.php");
include_once("../service/productDetailService.php");
include_once("../service/imageService.php");

//$proID = $_POST['proID'];
$proName = $_POST['pro_name'];
$proCode = $_POST['pro_code'];
$status = $_POST['status'];
$cate = $_POST['category'];
$price = $_POST['price'];
$relatedProduct = $_POST['relatedList'];
$des = $_POST['description'];

addProduct($cate, $proCode, $proName, $price, $des, $status);
$currentProID = getCurrentProID();

$img1 = $_FILES['img1'];
$oldID1 = $_POST['imageID1'];
$mainImageNewName = uploadImage($img1, '1', $oldID1, $currentProID);
reSize($mainImageNewName, $currentProID);

$img2 = $_FILES['img2'];
$oldID2 = $_POST['imageID2'];
uploadImage($img2, '2', $oldID2, $currentProID);

$img3 = $_FILES['img3'];
$oldID3 = $_POST['imageID3'];
uploadImage($img3, '3', $oldID3, $currentProID);

$detailList = getAllDetail();

while ($row = mysql_fetch_array($detailList)) {
    $newValue = $_POST[$row['PRD_ID']];
    if ($newValue != '') {
        addDetailValue($newValue, $row['PRD_ID'], $currentProID);
    }
}

if (isset($relatedProduct) && !empty($relatedProduct)) {
    foreach ($relatedProduct as $pro_pro_id) {
        addRelatedProduct($currentProID, $pro_pro_id);
    }
}

Header("Location: ../admin-page/new_product.php?add_message=ok");
?>